package com.lsh.airticket.controller.admin;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.airticket.dto.Result;
import com.lsh.airticket.dto.UserInfo;
import com.lsh.airticket.entity.Admin;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.AdminService;
import com.lsh.airticket.util.Constants;
import com.lsh.airticket.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author lsh
 * @date 2021/2/17
 **/

@RestController
@RequestMapping("/users")
public class AdminController {

    @Autowired
    AdminService adminService;

    @GetMapping("/admin/admin/adminList")
    public Result adminList() {
        List<Admin> adminList = adminService.findAdminList();
        return Result.result().addData("adminList", adminList);
    }

    /**
     * 分页查找
     * @param page
     * @return
     */
    @GetMapping("/admin/admin/page")
    public Result page(Integer page) {
        System.out.println(page+ "111111111111111111111");
        if (page == null || page == 0) {
            page = 1;
        }

        PageHelper.startPage(page, Constants.PAGE_SIZE);
        PageInfo<Admin> pageInfo = adminService.findByPage();
        return Result.result().addData("pageInfo", pageInfo);
    }



    /**
     * 管理员登录
     * @return
     */
    @PostMapping("/admin/login")
    public Result adminLogin(@RequestBody Admin admin) {
        if (admin == null || StrUtil.isEmpty(admin.getUsername()) || StrUtil.isEmpty(admin.getPassword())) {
            throw new UserException("用户名密码不能为空");
        }
        Admin login = adminService.login(admin);
        if (login == null) {
            throw new UserException("用户名密码错误");
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("username", login.getUsername());
        //添加标志位 1 管理员 2表示普通用户
        map.put("flag", 1);
        //生成token
        String token = JWTUtil.create(map);
        //token 用base64编码
        token = Base64.encode(token);
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(login.getUsername());
        userInfo.setAdminName(login.getAdminName());
        userInfo.setFlag(1);

        return Result.result().setMsg("登录成功！").addData("userInfo", userInfo).addData("token", token);
    }


    @PostMapping("/admin/register")
    public Result adminRegister(@RequestBody Admin admin) {
        int i = adminService.register(admin);
        if (i != 1) {
            throw new UserException("添加失败");
        }
        return Result.result().setMsg("添加成功");
    }

    /**
     * 根据主键删除一条数据
     */
    @GetMapping("/admin/deleteAdmin")
    public Result deleteAdmin(Integer adminId) {
        int delete = adminService.deleteUserById(adminId);
        if (delete != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }
}
